package com.app.miwawa.cui.eam;

import java.util.List;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.query.Criterion.Operator;
import org.framework.common.api.service.bo.CommonBoAware;
import org.framework.common.api.support.model.FormModel;
import org.framework.common.api.support.model.PageModel;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.cui.AbstractController;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.miwawa.api.eam.entity.StudentCheck;
import com.app.miwawa.api.eam.entity.StudentLeave;
import com.app.miwawa.api.eam.entity.view.StudentCalendar;
import com.app.miwawa.api.eam.entity.view.StudentLesson;
import com.app.miwawa.api.eam.service.bo.ClassesStudentBo;
import com.app.miwawa.api.eam.service.bo.StudentBo;

/**
 * 用户界面请求控制器类
 * <p>描述: 学员</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年2月3日 上午11:53:10
 * @version 1.0.2016
 */
@RestController
public class StudentController extends AbstractController{
	
	@Autowired
	private StudentBo studentBo;
	
	@Autowired
	private ClassesStudentBo classesStudentBo;
	
	@Resource(name = "defaultBo")
	private CommonBoAware<CommonEntityAware<String>, String> defaultBo;
	

	/**
	 * 分班转班前先删除之前的对应关系.
	 * @param dbname
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年2月10日 下午10:19:11
	 */
	@RequestMapping("/{dbname}/student/divide")
	public Object divide(@PathVariable final String dbname) throws Exception {
		// 参数检查
		if (check(dbname)) return error("请检查数据源或参数绑定源。");
		
		try{
			// 注册数据源
			registerDataSource(dbname);
			
			String studentids = request.getParameter("studentids");
			String classesids = request.getParameter("classesids");
			
			ProcessModel process = ProcessModel.factory();
			// 请假上限去重
			String sql = process.properties.getProperty("student.delete.leavelimit","[studentids]",studentids.split(","));
			sql = sql.replace("[classesids]", StringUtil.toSQLEscape(classesids.split(",")));
			process.jdbc.update(sql);
			
			// 分班去重
			classesStudentBo.remove(studentids, classesids);
			
			return success("学员分班成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return error("学员分班失败！", e);
		}
	}
	
	/**
	 * 签到.
	 * @param dbname
	 * @param entity
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午9:01:42
	 */
	@RequestMapping("/{dbname}/student/check/all")
	public Object check(@PathVariable final String dbname, @ModelAttribute final FormModel model) throws Exception {
		// 参数检查
		if (check(dbname)) return error("请检查数据源或参数绑定源。");

		try {
			// 注册数据源
			registerDataSource(dbname);
			
			List<StudentCheck> entities = model.getEntities();
			
			if(entities != null){
				for (StudentCheck check : entities) {
					studentBo.check(check);
				}
			}
			
			return success("学员签到成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return error("学员签到失败！", e);
		}
	}
	
	/**
	 * 请假.
	 * @param dbname
	 * @param entity
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午9:01:42
	 */
	@RequestMapping("/{dbname}/student/leave/save")
	public Object leave(@PathVariable final String dbname, @ModelAttribute final StudentLeave entity) throws Exception {
		// 参数检查
		if (check(dbname)) return success(false, "保存请假记录失败，数据源绑定错误！");

		try {
			// 注册数据源
			registerDataSource(dbname);
			
			// 保存请假记录
			studentBo.leave(entity);
			return success("学员请假成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return error("保存请假记录失败，服务器异常！", e);
		}
	}
	
	/**
	 * 取消签到.
	 * @param dbname
	 * @param entity
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午9:01:19
	 */
	@RequestMapping("/{dbname}/student/check/undo")
	public Object uncheck(@PathVariable final String dbname, @ModelAttribute final StudentLesson entity) throws Exception {
		// 参数检查
		if (check(dbname)) return error("请检查数据源或参数绑定源。");

		try {
			// 注册数据源
			registerDataSource(dbname);

			// 取消签到
			studentBo.uncheck(entity);
			
			return success("取消签到成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return error("取消签到失败！", e);
		}
	}
	
	/**
	 * 取消请假.
	 * @param dbname
	 * @param entity
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午9:01:07
	 */
	@RequestMapping("/{dbname}/student/leave/undo")
	public Object unleave(@PathVariable final String dbname, @ModelAttribute final StudentLesson entity) throws Exception {
		// 参数检查
		if (check(dbname)) return error("请检查数据源或参数绑定源。");

		try {
			// 注册数据源
			registerDataSource(dbname);
			
			// 取消请假
			studentBo.unleave(entity);
			
			return success("取消请假成功！");
		} catch (Exception e) {
			return error("取消请假失败！", e);
		}
	}
	
	/**
	 * 学员管理中的，查看签到历史.
	 * @param dbname
	 * @param entity
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月16日 下午4:56:16
	 */
	@RequestMapping("/{dbname}/student/check/history")
	public Object history(@PathVariable final String dbname, @ModelAttribute final StudentCalendar entity) throws Exception {
		// 参数检查
		if (check(dbname)) return error("请检查数据源或参数绑定源。");

		try {
			// 注册数据源
			registerDataSource(dbname);

			String item = entity.getItem();
			String invalid = entity.getInvalid();
			String studentId = entity.getStudentId();
			
			QueryModel query = new QueryModel(StudentCalendar.class, 0, 0);
			query.addAndQuery("item", item, Operator.EQ);
			query.addAndQuery("invalid", invalid, Operator.EQ);
			query.addAndQuery("studentId", studentId, Operator.EQ);
			
			PageModel<CommonEntityAware<String>> result = defaultBo.findByQuery(query);

			return result.getRows();
		} catch (Exception e) {
			return error("查看签到历史失败！", e);
		}
	}
}
